<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中英文分离工具（最终稳定版）</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            line-height: 1.6;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        textarea {
            width: 100%;
            height: 200px;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            font-size: 14px;
            font-family: monospace;
        }
        .btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .result-container {
            margin-top: 20px;
            display: flex;
            gap: 20px;
        }
        .result-box {
            flex: 1;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 10px;
            background-color: #f9f9f9;
        }
        .result-box h3 {
            margin-top: 0;
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
        }
        .result-box pre {
            margin: 0;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: monospace;
            font-size: 14px;
        }
        .instructions {
            margin-bottom: 15px;
            padding: 10px;
            background-color: #e7f3ff;
            border-left: 4px solid #2196F3;
            color: #003366;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>中英文内容分离工具</h1>
        
        <div class="instructions">
            <p><strong>说明：</strong>输入中英文混合的文本，每行一条。程序会自动分离中文和英文部分，并保留括号内的所有内容。例如：<br>
            输入：<code>电源Voltage(V/Hz)</code><br>
            输出：中文 → <code>电源(V/Hz)</code>，英文 → <code>Voltage(V/Hz)</code></p>
        </div>

        <textarea id="inputText" placeholder="请在此粘贴或输入您的中英文混合内容，每行一条：
电源Voltage(V/Hz)
功率Power(W)
频率Frequency(Hz)"></textarea>

        <button class="btn" onclick="processText()">分离内容</button>
        <button class="btn" onclick="clearAll()">清空内容</button>

        <div class="result-container">
            <div class="result-box">
                <h3>中文部分</h3>
                <pre id="chineseOutput"></pre>
            </div>
            <div class="result-box">
                <h3>英文部分</h3>
                <pre id="englishOutput"></pre>
            </div>
        </div>
    </div>

    <script>
        function processText() {
            const input = document.getElementById('inputText').value;
            const chineseLines = [];
            const englishLines = [];

            const lines = input.split('\n');

            for (let line of lines) {
                line = line.trim();

                if (line === '') {
                    chineseLines.push('');
                    englishLines.push('');
                    continue;
                }

                // === 1. 提取所有括号及其内容（关键：全局提取）===
                // 使用正则 /$$[^)]*$$/g 提取所有 (xxx) 结构
                const parenthesesMatch = line.match(/\([^)]*\)/g);
                let parenthesesContent = '';
                if (parenthesesMatch && parenthesesMatch.length > 0) {
                    // 去重并合并（防止多个相同括号）
                    const uniqueParens = [...new Set(parenthesesMatch)];
                    parenthesesContent = uniqueParens.join('');
                }

                // === 2. 提取中文：所有中文字符（汉字）===
                const chineseChars = line.match(/[\u4e00-\u9fff]+/g);
                let chinesePart = '';

                if (chineseChars && chineseChars.length > 0) {
                    chinesePart = chineseChars.join(''); // 拼接所有中文块
                }

                // === 3. 提取英文：从第一个英文字母开始，去除中文 ===
                let englishPart = '';
                const firstEnglishIndex = line.search(/[a-zA-Z]/);
                
                if (firstEnglishIndex !== -1) {
                    let engSegment = line.substring(firstEnglishIndex);
                    engSegment = engSegment.replace(/[\u4e00-\u9fff]/g, '').trim(); // 移除中文
                    engSegment = engSegment.replace(/^[^a-zA-Z0-9]*/, ''); // 清理开头符号
                    englishPart = engSegment;
                }

                // === 4. 【关键】最后统一添加括号内容 ===
                // 无论是否有内容，只要原始行有括号，就加到中英文末尾
                if (parenthesesContent) {
                    if (chinesePart) {
                        chinesePart += parenthesesContent;
                    } else {
                        chinesePart = parenthesesContent; // 无中文时只留括号
                    }

                    if (englishPart) {
                        // 避免英文部分重复括号
                        if (!englishPart.endsWith(parenthesesContent)) {
                            englishPart += parenthesesContent;
                        }
                    } else {
                        englishPart = parenthesesContent;
                    }
                }

                // 如果都为空，使用原始行
                if (!chinesePart && !englishPart) {
                    chinesePart = line;
                    englishPart = line;
                }

                chineseLines.push(chinesePart);
                englishLines.push(englishPart);
            }

            // 显示结果
            document.getElementById('chineseOutput').textContent = chineseLines.join('\n');
            document.getElementById('englishOutput').textContent = englishLines.join('\n');
        }

        function clearAll() {
            document.getElementById('inputText').value = '';
            document.getElementById('chineseOutput').textContent = '';
            document.getElementById('englishOutput').textContent = '';
        }
    </script>
</body>
</html>